package day08;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

import org.junit.Test;

/**
 * 使用异常捕获完成下述操作
 * 将控制台输入的每一行字符串使用缓冲字符输出流PrintWriter
 * 按行以GBK编码写入到文件note.txt中
 * @author Bonnie
 *
 */
public class Test02 {
	@Test
	public void test() {
		Scanner console = new Scanner(System.in);
		PrintWriter pw = null;
		try {
			pw = new PrintWriter(
					new OutputStreamWriter(
							new FileOutputStream("note.txt"), "GBK"), true);
			while (true) {
				System.out.print("请输入字符串(exit退出)：");
				String str = console.next();
				if (str.equals("exit")) {
					System.out.print("退出");
					break;
				} else {
					pw.println(str);
					System.out.println("打印一行成功！");
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			pw.close();
			console.close();
		}
		
		
	}
}
